﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.Generic;

namespace G.Controls
{
    [TemplatePart(Name = ItemsPanelName, Type = typeof(Canvas))]
    [TemplatePart(Name = CenteringTransformName, Type = typeof(TranslateTransform))]
    public class SlidingControl : Control
    {
        private const string ItemsPanelName = "ItemsPanel";
        private const string CenteringTransformName = "CenteringTransform";

        public static readonly DependencyProperty ItemTemplateProperty = DependencyProperty.Register("ItemTemplate", typeof(DataTemplate), typeof(SlidingControl), new PropertyMetadata(null));

        public DataTemplate ItemTemplate
        {
            get { return (DataTemplate)GetValue(ItemTemplateProperty); }
            set { SetValue(ItemTemplateProperty, value); }
        }

        public static readonly DependencyProperty ItemHeightProperty = DependencyProperty.Register("ItemHeight", typeof(double), typeof(SlidingControl), new PropertyMetadata(100.0));

        public double ItemHeight
        {
            get { return (double)GetValue(ItemHeightProperty); }
            set { SetValue(ItemHeightProperty, value); }
        }

        public static readonly DependencyProperty ItemWidthProperty = DependencyProperty.Register("ItemWidth", typeof(double), typeof(SlidingControl), new PropertyMetadata(100.0));

        public double ItemWidth
        {
            get { return (double)GetValue(ItemWidthProperty); }
            set { SetValue(ItemWidthProperty, value); }
        }

        public static readonly DependencyProperty SelectIndexProperty = DependencyProperty.Register("SelectIndex", typeof(int), typeof(SlidingControl), new PropertyMetadata(-1));

        public int SelectIndex
        {
            get { return (int)GetValue(SelectIndexProperty); }
            set { SetValue(SelectIndexProperty, value); }
        }

        public SlidingControl()
        {
            this.DefaultStyleKey = typeof(SlidingControl);
        }
        private Canvas itemsPanel;
        TranslateTransform centeringTransform;
        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();
            itemsPanel = GetTemplateChild(ItemsPanelName) as Canvas;
            centeringTransform = GetTemplateChild(CenteringTransformName) as TranslateTransform;
        }
        //public double W = 480;
        //public int SelectIndex = 0;

        public void Initial(IEnumerable<object> items)
        {
            if (itemsPanel == null)
            {
                this.ApplyTemplate();
            }
            //double offset = 0;
            double offset = (this.Width - this.ItemWidth) / 2;
            foreach (var item in items)
            {
                FrameworkElement f = this.ItemTemplate.LoadContent() as FrameworkElement;
                f.DataContext = item;
                f.Height = ItemHeight;
                f.Width = ItemWidth;
                this.itemsPanel.Children.Add(f);
                Canvas.SetLeft(f, offset);
                offset += ItemWidth;
            }
            this.SelectIndex = 0;
            if (SelectChanged != null)
                SelectChanged(this, EventArgs.Empty);
        }
        protected override void OnManipulationCompleted(ManipulationCompletedEventArgs e)
        {
            base.OnManipulationCompleted(e);
            if (Math.Abs(e.TotalManipulation.Translation.X) > 12)
            {
                if (e.TotalManipulation.Translation.X > 0)
                {
                    SelectIndex--;
                }
                else
                    SelectIndex++;
                if (SelectIndex < 0)
                    SelectIndex = 0;
                else if (SelectIndex >= this.itemsPanel.Children.Count)
                {
                    SelectIndex = this.itemsPanel.Children.Count - 1;
                }
                if (SelectChanged != null)
                    SelectChanged(this, EventArgs.Empty);
            }
         
            FrameworkElement f = this.itemsPanel.Children[SelectIndex] as FrameworkElement;
            centeringTransform.X = this.Width / 2 - Canvas.GetLeft(f) - ItemWidth / 2;
            //Storyboard sb = new Storyboard();
            //DoubleAnimation da = new DoubleAnimation();
            //da.Duration = TimeSpan.FromSeconds(0.2);
            //da.To = W / 2 - Canvas.GetLeft(f) - ItemWidth / 2;
            //Storyboard.SetTarget(da, centeringTransform);
            //Storyboard.SetTargetProperty(da, new PropertyPath("X"));
            //sb.Children.Add(da);
            //sb.Begin();
            //sb.Completed += (_s, _e) =>
            //{
            //    w.IsOpen = false;
            //};
        }
        public event EventHandler SelectChanged;
        Window w;
        TranslateTransform windowTrans = new TranslateTransform();
        protected override void OnManipulationDelta(ManipulationDeltaEventArgs e)
        {
            base.OnManipulationDelta(e);
            //windowTrans.X += e.DeltaManipulation.Translation.X;
            centeringTransform.X += e.DeltaManipulation.Translation.X;
        }

        protected override void OnManipulationStarted(ManipulationStartedEventArgs e)
        {
            base.OnManipulationStarted(e);
            //if (w == null)
            //{
            //    w = new Window();
            //    w.RenderTransform = windowTrans;
            //    var root = Application.Current.RootVisual as FrameworkElement;
            //    Point p = itemsPanel.GetRelativePosition(root);
            //    w.VerticalOffset = p.Y;
            //    w.HorizontalOffset = p.X;
            //}
            //try
            //{
            //    w.Content = new Image()
            //    {
            //        Source = new System.Windows.Media.Imaging.WriteableBitmap(this, null)
            //    };
            //}
            //catch { }
            //windowTrans.X = 0;
            //w.IsOpen = true;
        }
    }
}
